
/* helper-funcs for talking to feedgraph */

function validateTwitterTopic(topic) {

	switch (topic) {
	case "midMOvotes":
	case "Obama2012":
	case "RomneyRyan2012":
	case "news":
	case "senate":
	case "house":
	case "gov":
	case "ltgov":
	case "attgen":
	case "secstate":
	case "comedy":
	break;
	default: throw "invalid topic";
	}
}

function validateChatTopic(topic) {

	switch (topic) {
	case "president":
	case "us-senate":
	case "us-house":
	case "state-elections":
	case "ballot-issues":
	case "vip-room":
	break;
	default: throw "invalid topic";
	}
}

function validateNewsFeed(feed) {

	switch (feed) {
	case "election":
/*	case "all":
	case "news":
	case "sports":
	case "opinion":
	case "lifestyles":
	case "como-you-know":
	case "editors-choice":
	case "from-readers":
	case "neighborhoods":
	case "obituaries":
	case "special-sections":
	case "the-american-next":
*/	break;
	default: throw "invalid news feed";
	}
}

var bannedUsers = {};

function validateUserName(user) {

	if (user.length < 3) throw "user name too short";

	var match = user.match(/\w+/);

	if (match.length != 1 || match[0] != user) throw "user name contains illegal characters";

	if (bannedUsers[user] == true) throw "user has been banned!";
}

function sanitize(str) {

	if (!str) return "";

	return str.replace(/[<>]/g,"");
}

function postMessage(user, message, topic) {

	throw "party over.";

	validateUserName(user);
	message = sanitize(message);
	validateChatTopic(topic);

	var obj = {
		"User": user,
		"Message": message,
		"Topic": topic,
		"Time": new Date()
	};

	feedgraph.postobj("/party/user:" + user + "/posts", obj);
	feedgraph.postobj("/party/topic:" + topic + "/posts", obj);

}

function chatToHost(user, message) {

	throw "party over.";

	chatTo(user, "SternReuben", message);
}

function chatTo(user, target, message) {

	throw "party over.";

	validateUserName(user);
	validateUserName(target);
	message = sanitize(message);

	var obj = {
		"User": user,
		"Target": target,
		"Message": message,
		"Time": new Date(),
	};

	feedgraph.postobj("/party/user:"+target+"/inbox", obj);
	feedgraph.postobj("/party/user:"+user+"/inbox", obj);
}

/* callback fired each time a user posts a topical (chat room) message */
function registerMessageHandler(topic, callback) {

	validateChatTopic(topic);

	var validate = function (obj, meta) {
		try {
			validateUserName(obj.User);
		} catch (e) {
			if (console) console.log(e);
			return;
		}
		callback(obj, meta);
	}

	return feedgraph.watch("/party/topic:" + topic + "/posts", callback);
}

/* callback fired each time an article is posted to the Missourian */
function registerNewsHandler(feed, callback) {

	validateNewsFeed(feed);

	return feedgraph.watch("/news/feed:" + feed + "/articles", callback);
}

/* callback fired each time a tweet is indexed with the given "topic"
 *
 */
function registerTweetHandler(topic, callback) {

	validateTwitterTopic(topic);

	return feedgraph.watch("/tweets/topic:" + topic + "/tweets", callback);
}

function getLatestTweets(topic, callback) {

	validateTwitterTopic(topic);

	feedgraph.getfeed("/tweets/topic:" + topic + "/tweets", callback);
}

/* callback fired each time a private chat message is sent to the user */
function registerChatHandler(user, callback) {

	validateUserName(user);

	return feedgraph.watch("/party/user:" + user + "/inbox", callback);
}

function onBannedUsers(obj) {

	bannedUsers = obj;
}

function watchBannedUsers() {

	feedgraph.watch("/party/BANNED/users", onBannedUsers);
}

function onSignedIn(userObj) {

	feedgraph.postobj("/party/SIGNEDIN/users", userObj);
}

function registerSignedInHandler(callback) {

	return feedgraph.watch("/party/SIGNEDIN/users", callback);
}
